﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using Entetities;

public partial class UC_locsanpham : System.Web.UI.UserControl
{

    // Delegate declaration
    public delegate void linkClick(List<BoLoc> listboloc);
    // Event declaration 
    public event linkClick linkHandler;

    NhomBoLocBO nbl;
    BoLocBO bl;
    public List<BoLoc> listbl { get; set; }
    public int flag { get; set; }
    public int ctID { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        nbl = new NhomBoLocBO();
        bl = new BoLocBO();
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt = nbl.getNhomBoLoc(ctID);
            rptParent.DataSource = dt;
            rptParent.DataBind();
        }
    }

    protected void rptParent_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //tìm điều khiển rptchild trong 1 item của rptParent
        Repeater rpt_Con = (Repeater)e.Item.FindControl("rptChild");
        if (rpt_Con != null)
        {
            HiddenField HiddenParentID = (HiddenField)e.Item.FindControl("HiddenParentID");//lay ID theo parentID
            if (HiddenParentID != null)
            {
                int manhomboloc = int.Parse(HiddenParentID.Value.ToString());
                rpt_Con.DataSource = bl.getBoLoc(manhomboloc);
                rpt_Con.DataBind();
            }
        }
    }
    protected void LinkButton1_Command(object sender, CommandEventArgs e)
    {
        int id = Convert.ToInt32(e.CommandArgument);          
        BoLoc bl1 = bl.geBoLocEntity(id);       
        ViewState[bl1.Manhomboloc.ToString()] = bl1;
        display();
    }


    protected void Huy_Command(object sender, CommandEventArgs e)
    {
        string manhomboloc = e.CommandArgument.ToString();
        ViewState[manhomboloc] = null;
        display();
    }
    private void display()
    {
        string sql = null;
        List<BoLoc> listChon = new List<BoLoc>();
        foreach (StateItem i in ViewState.Values)
        {
            if (i != null)
            {
                BoLoc bo = (BoLoc)i.Value;
                if (bo != null)
                {
                    if (bo.Manhomboloc.ToString()!="")
                    {
                        sql += (" and Manhomboloc<>" + bo.Manhomboloc.ToString());
                        listChon.Add(bo);
                    }                    
                }
            }
        }
        listbl = listChon;
        if (sql==null)
        {
            rptParent.DataSource = bl.getBoLoc(ctID.ToString(), "");
            rptParent.DataBind();
            //linkHandler(listbl);
        }
        else
        {
            rptParent.DataSource = bl.getBoLoc(ctID.ToString(), sql);
            rptParent.DataBind();

            //kiem tra delegate
            //if (linkHandler != null)
            //{
            //    linkHandler(listbl);
            //}

        }
        linkHandler(listbl);
        rptChon.DataSource = listbl;
        rptChon.DataBind();

    }
}